/*
 * @Author: dingyuwen ding_yuwen@163.com
 * @Date: 2023-01-09 13:49:32
 * @LastEditTime: 2023-11-16 10:50:20
 * @LastEditors: DingYuwen 43669762+DingYuwen@users.noreply.github.com
 * @Description:
 */
// import ProfilePage from '@/pages/Profile/Page';
import { FC, lazy, ReactNode } from 'react';
import { Navigate, RouteObject } from 'react-router-dom';
import { useRoutes } from 'react-router-dom';

import Guard from './Guard';

import LazyElement from './LazyElement';

import AuthLayout from '@/common/components/layout/AuthLayout';
import DefaultLayout from '@/common/components/layout/DefaultLayout';
import { FullPageCustomLoader } from '@/common/components/NewLoading';
import { APP_NAME } from '@/config/constants';
import Profile from '@/pages/Profile';
const AutoLogin = lazy(() => import('@/pages/Home/AutoLogin'));
const ColorSeparator = lazy(() => import('@/pages/ColorSeparator'));
const NotFound = lazy(() => import('@/pages/Error/NotFound'));
const Home = lazy(() => import('@/pages/Home'));
const Login = lazy(() => import('@/pages/Login'));
const Training = lazy(() => import('@/pages/Training'));
const Signup = lazy(() => import('@/pages/Signup'));
const ForgotPassword = lazy(() => import('@/pages/ForgotPassword'));
const Aigc = lazy(() => import('@/pages/Aigc'));

export interface RouteMeta {
  auth?: boolean;
  title?: string;
  useLayoutGuard?: boolean;
  useRouteGuard?: boolean;
  [k: string]: any;
}

export interface RouteObj {
  path: string;
  component?: ReactNode;
  element?: ReactNode;
  loading?: ReactNode;
  redirect?: string;
  meta?: RouteMeta;
  children?: RouteObj[];
}

const routesList: RouteObj[] = [
  {
    path: '*',
    component: <NotFound />,
    meta: {
      auth: false,
      title: '404',
    },
  },

  {
    path: '',
    component: <DefaultLayout />,
    meta: {
      auth: false,
    },
    children: [
      {
        path: '/',
        component: <Home />,
        meta: {
          title: APP_NAME,
          auth: false,
        },
      },
      {
        path: 'gallery',
        redirect: '/profile',
      },
      {
        path: 'profile',
        meta: {
          title: `个人中心 - ${APP_NAME}`,
          auth: true,
        },
        component: <Profile />,
      },
      {
        path: 'training',
        component: <Training />,
        meta: {
          title: `模型训练 - ${APP_NAME}`,
          auth: true,
        },
      },
      {
        path: 'colorSeparate',
        component: <ColorSeparator />,
        meta: {
          title: `图片分色 - ${APP_NAME}`,
          auth: true,
        },
      },
    ],
  },

  {
    path: '',
    component: <AuthLayout />,
    meta: {
      auth: false,
    },
    children: [
      {
        path: 'signin',
        component: <Login />,
        meta: {
          title: `登录 - ${APP_NAME}`,
          auth: false,
        },
      },
      {
        path: 'autoAuth',
        component: <AutoLogin />,
        meta: {
          title: `自动登录 - ${APP_NAME}`,
          auth: false,
        },
      },
      {
        path: 'registry',
        component: <Signup />,
        meta: {
          title: `注册 - ${APP_NAME}`,
          auth: false,
        },
      },
      {
        path: 'forgotPassword',
        component: <ForgotPassword />,
        meta: {
          title: `忘记密码 - ${APP_NAME}`,
          auth: false,
        },
      },
    ],
  },

  {
    path: 'aigc',
    component: <Aigc />,
    meta: {
      auth: true,
      title: APP_NAME,
    },
    loading: <FullPageCustomLoader />,
  },
];

// 使用layout守卫或者路由守卫
function transRoutes(routes: RouteObj[]): RouteObject[] {
  const list: RouteObject[] = [];
  routes.forEach((route) => {
    const {
      component,
      meta = {},
      redirect,
      children,
      loading,
      path,
    } = { ...route };
    const returnObj: RouteObject = { path };

    if (redirect) {
      returnObj.element = <Navigate to={redirect} replace={true} />;
    } else if (component && meta && meta.auth) {
      returnObj.element = (
        <Guard
          element={
            <LazyElement
              title={meta.title}
              loading={loading}
              component={component}
            />
          }
          meta={meta}
        />
      );
    } else if (component) {
      returnObj.element = (
        <LazyElement
          title={meta.title}
          loading={loading}
          component={component}
        />
      );
    }

    if (children) {
      returnObj.children = transRoutes(children);
    }

    list.push(returnObj);
  });
  return list;
}
const AppRoutes: FC = () => useRoutes(transRoutes(routesList));

export default AppRoutes;
